//
//  BaseTabBarController.m
//  XDLBaseProject
//
//  Created by Xiedonglai on 17/1/11.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseNavigationController.h"
#import "BaseViewController.h"
#import "HomeViewController.h"
#import "MeViewController.h"

@interface BaseTabBarController ()

@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setProperty];
    [self setSVProgressHUD];
    [self removeTabbarTopLine];
    [self setViewControllers];
}

- (void)setProperty {
    [[UITabBar appearance] setTintColor:[AppColor getColor_MAIN]];
    [[UITabBar appearance] setUnselectedItemTintColor:UIColorFromHex(0x9a9a9a)];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:(UIControlStateNormal)];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:(UIControlStateSelected)];
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = self.tabBar.standardAppearance;
        NSMutableParagraphStyle *par = [[NSMutableParagraphStyle alloc]init];
        par.alignment = NSTextAlignmentCenter;
        appearance.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(kScreenW, 49)];
        appearance.shadowImage = [UIImage new];
        appearance.shadowColor = [UIColor clearColor];
        self.tabBar.standardAppearance = appearance;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = appearance;
        }

        UITabBarItemAppearance *itemAppearance = [[UITabBarItemAppearance alloc] init];
        itemAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, -2);
        itemAppearance.normal.titleTextAttributes = @{NSParagraphStyleAttributeName : par};
        itemAppearance.selected.titlePositionAdjustment = UIOffsetMake(0, -2);
        itemAppearance.selected.titleTextAttributes = @{NSParagraphStyleAttributeName : par};
        appearance.stackedLayoutAppearance = itemAppearance;
    }
}

- (void)setViewControllers {
    
    NSArray *dataArr = @[
                        @{
                            @"class":[HomeViewController class],
                            @"title":@"首页",
                            @"imageName":@"tab_academic_0",
                            @"selectedImageName":@"tab_academic_1"
                        },
                        @{
                            @"class":[MeViewController class],
                             @"title":@"我的",
                             @"imageName":@"tab_me_0",
                             @"selectedImageName":@"tab_me_1"
                             },
                        ];
    for (NSDictionary *dataDic in dataArr) {
        Class class = dataDic[@"class"];
        NSString *title = dataDic[@"title"];
        NSString *imageName = dataDic[@"imageName"];
        NSString *selectedImageName = dataDic[@"selectedImageName"];
        
        BaseNavigationController *nav = [self itemChildViewController:class title:title imageName:imageName selectedImageName:selectedImageName];
//        nav.navigationBar.topItem.title = title;
        [self addChildViewController:nav];
    }
}

- (void)setSVProgressHUD {
    //SVProgressHUDMaskType 设置显示的样式
//    [SVProgressHUD setMinimumDismissTimeInterval:1.2f];
//    [SVProgressHUD setFont:[AppFont getFont_14]];
//    [SVProgressHUD setRingThickness:Width_Real(2)];
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
//    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
//    [SVProgressHUD setDefaultMaskType:(SVProgressHUDMaskTypeBlack)];
    
    [SVProgressHUD setMinimumDismissTimeInterval:1.2f];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
}

- (BaseNavigationController *)itemChildViewController:(Class)class title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    BaseViewController *vc = [class new];
    [vc.tabBarItem setTitle:title];
    [vc.tabBarItem setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]];
    [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:selectedImageName] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]];
    [vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -2)];
//    [vc.tabBarItem setImageInsets:(UIEdgeInsetsMake(-5, 0, 5, 0))];
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
    
    return nav;
}

- (void)removeTabbarTopLine {
    [self.tabBar setShadowImage:[UIImage new]];
    [self.tabBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(kScreenW, 49)]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
